//
//  ViewController.m
//  UI24作业MD5
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+HLMD5.h"

#define kEncryption @"http://tool.zzblo.com/api/md5/encrypt"
#define kDecryption @"http://tool.zzblo.com/api/md5/decrypt"

@interface ViewController ()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *encryptTextFiled;

@property (weak, nonatomic) IBOutlet UITextField *decodeTextFiled;

@property (nonatomic, copy) NSString *md5Str;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)encrypt:(id)sender {
    NSString *param = [NSString stringWithFormat:@"text=%@", _encryptTextFiled.text];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:kEncryption parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (IBAction)decode:(id)sender {
    NSString *param = [NSString stringWithFormat:@"secret=%@", _decodeTextFiled.text];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:kEncryption parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", result);
        NSNumber *status = [result objectForKey:@"status"];
        if (status.integerValue == 200) {
            _encryptTextFiled.text = [result objectForKey:@"text"];
        } else if (status.integerValue == 1004) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"没有找到相应解密, 请先加密" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *confimAciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    _decodeTextFiled.text = @"";
                }];
                [alert addAction:confimAciton];
                [self presentViewController:alert animated:YES completion:^{
                    
                    _encryptTextFiled.text = _md5Str;
                    
                }];
            });
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

- (IBAction)change:(id)sender {
    _md5Str = _decodeTextFiled.text;
    _decodeTextFiled.text = [_decodeTextFiled.text stringWith32BitMD5Lower];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
